//
//  HomePersonAlertView.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/24.
//

#import "HomePersonAlertView.h"
#import "HomeGetDetailAllModel.h"
#import "HomeAreaAlertViewTableViewCell.h"

@interface HomePersonAlertView ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger Index;
    UIView * topV;
}
@property (nonatomic,strong) UIView * follewView;
@property (nonatomic,strong) UITableView * oneTableView;
@property (nonatomic,strong) NSMutableArray * oneDataSouce;
@property (nonatomic,strong) UITableView * twoTableView;
@property (nonatomic,strong) NSMutableArray * twoDataSouce;




@end

@implementation HomePersonAlertView

- (instancetype)init
{
    self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        
        [self setUpUI];
        
    }
    return self;
}



-(void)setModel:(HomeMySlipleModel *)model{
    _model = model;
    self.oneDataSouce = [NSMutableArray arrayWithArray:[HomeMySlipleOrgListModel mj_objectArrayWithKeyValuesArray:self.model.orgList]];
    NSMutableArray *personListArr = [NSMutableArray arrayWithArray:[HomeMySlipleUserListModel mj_objectArrayWithKeyValuesArray:self.model.userList]];

    [self.oneDataSouce enumerateObjectsUsingBlock:^(HomeMySlipleOrgListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {

        [personListArr enumerateObjectsUsingBlock:^(HomeMySlipleUserListModel * obj1, NSUInteger idx1, BOOL * _Nonnull stop) {
            if ([obj.id isEqualToString:obj1.orgId]){
                [obj.child addObject:obj1];
            }

        }];

    }];
    [self.oneTableView reloadData];
}


-(void)setUpUI{
    
    
    topV = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT , SCREEN_WIDTH, 40)];
    topV.backgroundColor = COLOR_RGBA(242, 242, 242, 1.0);
    [self addSubview:topV];

    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, 0, 100, 40);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [cancelBtn addTarget:self action:@selector(respondsTo_checkCancel) forControlEvents:UIControlEventTouchUpInside];
    [topV addSubview:cancelBtn];

    UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    yesBtn.frame = CGRectMake(SCREEN_WIDTH - 100, 0, 100, 40);
    [yesBtn setTitle:@"完成" forState:UIControlStateNormal];
    [yesBtn setTitleColor:COLOR_RGBA(69, 186, 155, 1) forState:UIControlStateNormal];
    [yesBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [yesBtn addTarget:self action:@selector(respondsTo_checkyes) forControlEvents:UIControlEventTouchUpInside];
    [topV addSubview:yesBtn];
    
    
    self.follewView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topV.frame), SCREEN_WIDTH, 207)];
    self.follewView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.follewView];
    
    [self.follewView addSubview:self.oneTableView];
    [self.follewView addSubview:self.twoTableView];
    
    [self.oneTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.follewView.mas_top);
        make.bottom.left.equalTo(self.follewView);
        make.width.equalTo(@(SCREEN_WIDTH / 3));
    }];
    
    [self.twoTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.oneTableView.mas_right);
        make.bottom.equalTo(self.follewView);
        make.top.equalTo(self.oneTableView.mas_top);
        make.width.equalTo(@(SCREEN_WIDTH - (SCREEN_WIDTH / 3)));
    }];

    
    
    [UIView animateWithDuration:0.25 animations:^{
        topV.frame = CGRectMake(0, SCREEN_HEIGHT - 247, SCREEN_WIDTH, 40);
        self.follewView.frame = CGRectMake(0, CGRectGetMaxY(topV.frame), SCREEN_WIDTH, 207);
    }];
    
    
    
    
}

#pragma amrk -- 点击事件
-(void)respondsTo_checkCancel{
    [self remove];
}

-(void)respondsTo_checkyes{
    [self remove];
    NSMutableArray * temp = @[].mutableCopy;
    NSMutableArray * showArr = @[].mutableCopy;

    // 整理数据
    [self.oneDataSouce enumerateObjectsUsingBlock:^(HomeMySlipleOrgListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.select){
            NSMutableDictionary * dic = @{}.mutableCopy;
            [dic setValue:obj.id forKey:@"id"];
            [dic setValue:obj.name forKey:@"name"];
            [temp addObject:dic];
            [showArr addObject:obj.name];
        }
    }];
    
    [self.twoDataSouce enumerateObjectsUsingBlock:^(HomeMySlipleUserListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.select){
            NSMutableDictionary * dic = @{}.mutableCopy;
            [dic setValue:obj.id forKey:@"id"];
            [dic setValue:obj.nickname forKey:@"nickname"];
            [dic setValue:obj.nickname forKey:@"name"];
            [temp addObject:dic];
            [showArr addObject:obj.nickname];
        }
    }];
    
    _personBlock(temp,showArr);
    
    
}

- (void)remove {

    [UIView animateWithDuration:0.25 animations:^{

        self->topV.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 40);
        self.follewView.frame = CGRectMake(0, CGRectGetMaxY(self->topV.frame), SCREEN_WIDTH, 207);

    } completion:^(BOOL finished) {

        [self removeFromSuperview];
    }];

}





#pragma mark  -- UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (tableView == self.oneTableView){
            return self.oneDataSouce.count;
        }else if (tableView == self.twoTableView){
            return self.twoDataSouce.count;
        }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
       
    HomeAreaAlertViewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeAreaAlertViewTableViewCell"];
    
    if (!cell){
        cell = [[HomeAreaAlertViewTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeAreaAlertViewTableViewCell"];
        
    }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (tableView == self.oneTableView){
                HomeMySlipleOrgListModel * model = self.oneDataSouce[indexPath.row];
                cell.msgLabel.text = model.name;
                if (model.select){
                    cell.gouImg.image = [UIImage imageNamed:@"勾选"];
                }else{
                    cell.gouImg.image = [UIImage imageNamed:@""];
                }
                
                
            }
            if (tableView == self.twoTableView){
                HomeMySlipleUserListModel * model = self.twoDataSouce[indexPath.row];
                cell.msgLabel.text = model.nickname;
                if (model.select){
                    cell.gouImg.image = [UIImage imageNamed:@"勾选"];
                }else{
                    cell.gouImg.image = [UIImage imageNamed:@""];
                }
                
            }

           
        
        
        return  cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(45);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.oneTableView){

        [self.oneDataSouce enumerateObjectsUsingBlock:^(HomeMySlipleOrgListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.select = NO;
        }];
        
        HomeMySlipleOrgListModel * model = self.oneDataSouce[indexPath.row];
        model.select = YES;
        self.twoDataSouce = [NSMutableArray arrayWithArray:[HomeMySlipleUserListModel mj_objectArrayWithKeyValuesArray:model.child]];
        
        [self.oneTableView reloadData];
        [self.twoTableView reloadData];
        
    }else if (tableView == self.twoTableView){
        
        [self.twoDataSouce enumerateObjectsUsingBlock:^(HomeMySlipleUserListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.select = NO;
        }];
        HomeMySlipleUserListModel * model = self.twoDataSouce[indexPath.row];
        model.select = YES;
        [self.twoTableView reloadData];
        
    }
    
}







#pragma amrk -- 懒加载
-(UITableView *)oneTableView{
    if(!_oneTableView){
        _oneTableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _oneTableView.dataSource = self;
        _oneTableView.delegate = self;
        _oneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _oneTableView.showsVerticalScrollIndicator = false;
        _oneTableView.showsHorizontalScrollIndicator = false;
    }
    return _oneTableView;
}
-(UITableView *)twoTableView{
    if(!_twoTableView){
        _twoTableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _twoTableView.dataSource = self;
        _twoTableView.delegate = self;
        _twoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _twoTableView.showsVerticalScrollIndicator = false;
        _twoTableView.showsHorizontalScrollIndicator = false;
    }
    return _twoTableView;
}


-(NSMutableArray *)oneDataSouce{
    if (!_oneDataSouce){
        _oneDataSouce = [NSMutableArray array];
    }
    return _oneDataSouce;
}

-(NSMutableArray *)twoDataSouce{
    if (!_twoDataSouce){
        _twoDataSouce = [NSMutableArray array];
    }
    return _twoDataSouce;
}



@end
